package eu.dominicum.ft.pd.boundary.ejb;

import eu.dominicum.ft.pd.Country;
import eu.dominicum.ft.pd.Location;
import eu.dominicum.ft.pd.boundary.LocationManagement;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

/**
 * An EJB implementation of the {@link eu.dominicum.ft.pd.boundary.LocationManagement} interface.
 *
 * @author Harry Klerks
 * @since 2013M1
 */
@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public class LocationManagementEJB implements LocationManagement {

    /**
     * The entity manager.
     */
    @PersistenceContext(name = "pu_ft")
    private EntityManager entityManager;


    @Override
    public List<Location> findAllLocations() {

        Query namedQuery = entityManager.createNamedQuery("Location.findAll", Location.class);
        return (List<Location>) namedQuery.getResultList();
    }

    @Override
    public List<Country> findAllCountries() {

        Query namedQuery = entityManager.createNamedQuery("Country.findAll", Country.class);
        return (List<Country>) namedQuery.getResultList();
    }
}
